package com.example.demo.controller;

import java.io.IOException;
import java.io.Writer;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.demo.pojo.Result;
import com.example.demo.service.IResultService;
import com.google.gson.Gson;

@Controller
@RequestMapping("/Result")
public class ResultController {

	@Resource
	private IResultService ResultService;
	

	@RequestMapping("/selectResult")
	public void selectResult(HttpServletResponse response, HttpServletRequest request, Model model, Result Result, Integer mid) throws IOException {
		List<Result> list = ResultService.selectResult();
		Gson gson = new Gson();
		String json = gson.toJson(list);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}

	@RequestMapping("/viewResult")
	public void viewpaper(HttpServletResponse response, HttpServletRequest request, Model model, Result Result, Integer mid) throws IOException {
		List<Result> list = ResultService.selectResultByMid(mid);
		Gson gson = new Gson();
		String json = gson.toJson(list);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}
	
	@RequestMapping("/selectResultByNum")
	public void selectResultByNum(HttpServletResponse response,Result Result, Integer rnum, Integer mid) throws IOException {
		System.out.println("rnum:"+rnum+" mid:"+mid);
		Result= ResultService.selectResultByNum(mid, rnum);
		Gson gson = new Gson();
		String json = gson.toJson(Result);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}

	@RequestMapping("/selectResultById")
	public void selectResultById(HttpServletResponse response,Result Result,Integer id) throws IOException {
		Result= ResultService.selectResultById(id);
		Gson gson = new Gson();
		String json = gson.toJson(Result);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}

	@RequestMapping("/addResult")
	public void addResult(HttpServletResponse response,Result Result, Integer rsnum, Integer renum, String result, Integer mid) throws IOException {
		Result.setMid(mid);
		Result.setRenum(renum);
		Result.setResult(result);
		Result.setRsnum(rsnum);
		ResultService.insertResult(Result);
		Gson gson = new Gson();
		String json = gson.toJson(Result);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}




	@RequestMapping("/updateResult")
	public void updateResult( HttpServletResponse response,Result Result, Integer id, Integer rsnum, Integer renum, String result, String level) throws IOException {
		Result.setId(id);
		Result.setRenum(renum);
		Result.setResult(result);
		Result.setRsnum(rsnum);
		Result.setLevel(level);
		ResultService.updateMreultById(Result);
		Gson gson = new Gson();
		String json = gson.toJson(Result);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}


	@RequestMapping("/deleteResult")
	public void deleteResult(HttpServletResponse response,Integer id) throws IOException {
		ResultService.deleteResultById(id);
		Gson gson = new Gson();
		String json = gson.toJson("");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}

}
